<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<meta name="apple-mobile-web-app-capable" content="yes"><!--删除默认的苹果工具栏和菜单栏-->
		<meta name="apple-mobile-web-app-status-bar-style" content="black" /><!--状态栏显示为黑色-->
		<meta name="format-detection" content="telephone=no" /><!--电话号码不被显示为拨号连接-->
		<title>融资融券交易终端</title>
		<link rel="stylesheet" href="<?php echo STATIC_SERVER; ?>/mobile/layer/layer.css" id="layui_layer_layercss"/>
		<link rel="stylesheet" href="<?php echo STATIC_SERVER; ?>/mobile/css/weui.css" />
		<link rel="stylesheet" href="<?php echo STATIC_SERVER; ?>/mobile/css/style.css" />
		<script src="<?php echo STATIC_SERVER; ?>/mobile/js/jquery-1.12.1.min.js"></script>
		<script src="<?php echo STATIC_SERVER; ?>/mobile/layer/layer.js"></script>
	</head>
	<body>
		<div class="xinsu-section">
			<div class="xinsu-equity">
				<ul class="xinsu-cross-title">
					<li <?php if ($common_info['action'] == 'index') { ?>class="xinsu-current"<?php } ?>><a href="/stock">持仓单</a></li>
					<li <?php if ($common_info['action'] == 'trust') { ?>class="xinsu-current"<?php } ?>><a href="/stock/trust">委托单</a></li>
					<li <?php if ($common_info['action'] == 'delivery') { ?>class="xinsu-current"<?php } ?>><a href="/stock/delivery">交割单</a></li>
					<div class="xinsu-clear"></div>
				</ul>
				<div class="xinsu-equity-con">
					<?php if (empty($list) == false) { ?>
					<?php foreach ($list as $val) { ?>
					<div class="xinsu-equity-list" id="xinsu-stock-<?php echo $val['id']; ?>" data="<?php echo $val['id']; ?>">
						<div class="xinsu-equity-list-title">
							<span>编号：<?php echo $val['id']; ?></span>
							<p>建仓时间：<?php echo date("Y-m-d H:i:s", $val['bull_time']); ?></p>
						</div>
						<ul>
							<li>
								<p>股票名称：<?php echo $val['stock_name']; ?></p>
								<p>买入价：<?php echo $val['bull_price']; ?></p>
								<p>最新价：<span class="xinsu-color-red" id="xinsu-price-<?php echo $val['id']; ?>">--</span></p>
							</li>
							<li>
								<p>类型：<?php if ($val['bull_type'] == 1) { ?><span class="xinsu-color-red">融资↑</span><?php } else { ?><span class="xinsu-color-green">融券↓</span><?php } ?>（<?php echo $val['bull_num']; ?>）</p>
								<p>盈亏：<span id="xinsu-gain-<?php echo $val['id']; ?>">--</span></p>
								<p>结算天/费：<span class="xinsu-color-red" id="xinsu-save-<?php echo $val['id']; ?>">--</span></p>
							</li>
							<div class="xinsu-clear"></div>
						</ul>
						<div>
							<div style="background-color: #e66e61;text-align: center;margin: 5px;border-radius: 10px;height: 44px;line-height: 44px;">
								<a id="xinsu-mange-<?php echo $val['id']; ?>" class="xinsu-sell xinsu-block-sell" href="javascript:;" data="<?php echo $val['id']; ?>" data-num="共<?php echo $val['bull_num']; ?>（股） 已委托 <?php echo $val['bull_num'] - $val['can_sell_num'] - $val['sell_num']; ?>（股） 可卖：<?php echo $val['can_sell_num']; ?>（股）" data-bull-time="<?php echo date('Y-m-d H:i:s',$val['bull_time']); ?>" data-price="<?php echo $val['bull_price']; ?>" data-bulltype="<?php echo $val['bull_type']; ?>" data-stock="<?php echo $val['stock']; ?>" data-cannum="<?php echo $val['can_sell_num']; ?>" data-stockname="<?php echo $val['stock_name']; ?>"><?php if ($val['bull_type'] == 1) { ?><span  style="color: white;font-size: 16px;">卖券还款<span><?php } else { ?><span
                                        style="color: white;font-size: 16px;">买券还券
                                    <span><?php } ?>
								</a>
							</div>
						</div>
					</div>
					<?php } ?>
					<?php } ?>
				</div>
			</div>
		</div>
		<script>
			$(function(){
				get_stock();
			})
			var stock = 0;
			
			//获取最新价格
			function get_stock() {
				if (stock >= 0) {
					var url = "/stock/ajax_get_bull";
					var data = "";
					$(".xinsu-equity-list").each(function(){
						var tmp = $(this).attr("data");
						if (tmp != undefined) {
							data += "," + tmp;
						}
					})
					http_request_post(url, {data:data}, function(res) {
						var result = res.data;
						if (res.status == 1) {
							//列表
							$.each(result, function(key, val){
								var bull_type = $("#xinsu-mange-" + key).attr("data-bulltype");
								if (val.price > 0) {
									$("#xinsu-price-" + key).html(val.price);
									if (val.gain > 0) {
										$("#xinsu-gain-" + key).addClass("xinsu-color-red").removeClass("xinsu-color-green");
									} else {
										$("#xinsu-gain-" + key).addClass("xinsu-color-green").removeClass("xinsu-color-red");
									}
									$("#xinsu-gain-" + key).html(val.gain);
									$("#xinsu-save-" + key).html(val.save);
									$("#xinsu-mange-" + key).addClass("xinsu-block-sell").removeClass("xinsu-block-disabled");
									if (bull_type == 1) {
										$("#xinsu-mange-" + key).html('<span style="color: white;font-size: 16px;">卖券还款<span>');
									} else {
										$("#xinsu-mange-" + key).html('<span style="color: white;font-size: 16px;">买券还券<span>');
									}
								} else {
									$("#xinsu-price-" + key).html("--");
									$("#xinsu-gain-" + key).html("--");
									$("#xinsu-save-" + key).html("--");
									$("#xinsu-mange-" + key).html("停牌");
									$("#xinsu-mange-" + key).removeClass("xinsu-block-sell").addClass("xinsu-block-disabled");
								}
							})
							
							setTimeout("get_stock();", 1000);
						}
					});
				}
				
			}

			//平仓
			$(".xinsu-block-sell").click(function(){
				var id = $(this).attr("data");
				var num = $(this).attr("data-num");
				var stock = $(this).attr("data-stock");
				var stock_name = $(this).attr("data-stockname");
				var can_sell_num = $(this).attr("data-cannum");
				var bull_price = $(this).attr("data-price");
				var bull_time = $(this).attr("data-bull-time");
				var bull_type = $(this).attr("data-bulltype");
				if (bull_type == 1) {
					var title = "卖券还款";
					bull_type = '<span class="xinsu-color-red">融资↑</span>';
				} else {
					var title = "买券还券";
					bull_type = '<span class="xinsu-color-green">融券↓</span>';
				}
				var price = $("#xinsu-price-" + id).html();
				if (price == "--") {
					layer.msg("该股票已停牌，禁止交易");return false;
				}
				//弹出卖出
				layer.open({
					type: 1,
					title: title,
					area: ['90%', ''],
					btn:['确定', '取消'],
					shadeClose: true, //点击遮罩关闭
					content: '<div class="xinsu-sell-open"><form class="weui-cells xinsu-cells" method="post"><div class="weui-cell"><div class="weui-cell__bd"><p>股票名称：'+stock_name+'</p></div></div><div class="weui-cell"><div class="weui-cell__bd"><p>股票代码：'+stock+'</p></div></div><div class="weui-cell"><div class="weui-cell__bd"><p>持仓时间：'+bull_time+'</p></div></div><div class="weui-cell"><div class="weui-cell__bd"><p>持仓数量：'+num+'</p></div></div><div class="weui-cell"><div class="weui-cell__bd"><p>股票类型：'+bull_type+'</p></div></div><div class="weui-cell"><div class="weui-cell__bd"><p>买入价格：<span class="xinsu-color-green">'+bull_price+' 元/股</span></p></div></div><div class="weui-cell"><div class="weui-cell__bd"><p>当前价格：<span class="xinsu-color-red">'+price+'/股</span></p></div></div><div class="weui-cell xinsu-cell"><div class="weui-cell__fl">	<label class="weui-label">卖出价格：</label></div><div class="weui-cell__bd"><input type="radio" name="price_type" value="0" onclick="javascript:$(\'#xinsu-sell-price\').hide();" checked /><span class="xinsu-color-red">市价 </span><input type="radio" name="price_type" value="1" onclick="javascript:$(\'#xinsu-sell-price\').show();" /><span class="xinsu-color-green"> 委托价 </span></div></div><div class="weui-cell xinsu-cell" id="xinsu-sell-price" style="display:none;"><div class="weui-cell__fl"><label class="weui-label">委托价格：</label></div><div class="weui-cell__bd"><input type="text" name="sell_price" id="sell_price" value="'+price+'" class="weui-input xinsu-color-red" placeholder="请输入委托价格" /></div></div><div class="weui-cell xinsu-cell"><div class="weui-cell__fl"><label class="weui-label">卖出数量：</label></div><div class="weui-cell__bd"><input type="text" id="sell_num" name="sell_num" class="weui-input" value="'+can_sell_num+'" placeholder="最大卖出数量'+can_sell_num+'股" /></div></div></form></div>',
					yes:function(index, layers){
						var price_type = $("input[name='price_type']:checked").val();
						var sell_num = $("#sell_num").val();
						var sell_price = $("#sell_price").val();
						//交易类型
						if (price_type == 0) {
							var rmsg = "市价平仓处理成功！";
						} else {
							var rmsg = "限价平仓委托处理成功！";
						}
						http_request_post("/stock/ajax_set_sell", {id:id, sell_num:sell_num, sell_price:sell_price, price_type:price_type, token:token}, function(result){
							layer.closeAll();
							layer.alert(rmsg, {icon:1, closeBtn:0}, function(){
								location.reload();
							});
						});
					}
					
				});
			})
		</script>
		<div class="weui-tabbar xinsu-tabbar">
		    <a href="/stock/bull" class="weui-tabbar__item <?php if ($common_info['action'] == 'bull') { ?>xinsu-current<?php } ?>">
			    <div class="weui-tabbar__icon">
			        <i class="xinsu-icon-font xinsu-icon-deal"></i>
			    </div>
		      	<p class="weui-tabbar__label">交易</p>
		    </a>
		    <a href="/stock/index" class="weui-tabbar__item <?php if ($common_info['action'] != 'bull' && $common_info['controller'] == 'stock') { ?>xinsu-current<?php } ?>">
			    <div class="weui-tabbar__icon">
			        <i class="xinsu-icon-font xinsu-icon-search"></i>
			    </div>
			    <p class="weui-tabbar__label">查询</p>
		    </a>
		    <a href="/user/asset" class="weui-tabbar__item <?php if ($common_info['action'] == 'asset' || $common_info['controller'] == 'atm') { ?>xinsu-current<?php } ?>">
		      	<div class="weui-tabbar__icon">
		        	<i class="xinsu-icon-font xinsu-icon-asset"></i>
		      	</div>
		      	<p class="weui-tabbar__label">资产</p>
		    </a>
		    <a href="/" class="weui-tabbar__item <?php if (($common_info['action'] != 'asset' && $common_info['controller'] == 'user') || $common_info['controller'] == 'index') { ?>xinsu-current<?php } ?>">
		      	<div class="weui-tabbar__icon">
		        	<i class="xinsu-icon-font xinsu-icon-user"></i>
		      	</div>
		      	<p class="weui-tabbar__label">我的</p>
		    </a>
		</div>
	</body>
	<script>
		//底部导航隐藏
		var original = document.documentElement.clientHeight;
		window.addEventListener("resize", function() {
			var resizeHeight = document.documentElement.clientHeight;
			if(resizeHeight != original) {
				$('.xinsu-tabbar').hide()
			} else {
				$('.xinsu-tabbar').show()
			}
		});
		var token = "<?php echo $token; ?>";
		//退出登录
		function logout() {
			layer.confirm('您确定要退出？', {icon: 3}, function () {
				stock = system_info = -1
				var url = "/user/logout";
				layer.closeAll();
				http_request_post(url, null, function(res) {
					layer.msg(res.msg, {time: 800}, function () {
						//跳转到登录页面
						location.href = "/user/login";
					});
				});
			});
		}
		
		//提交表单
        var sub = 0;
		$("form").submit(function () {
			//判断提交方式：get-搜索专用，post-保存专用
			var method = $(this).attr("method");
			if (method == "post") {
				//保存专用
				var rmsg = "";
				var data = $(this).serialize();
				if ("<?php echo $common_info['action']; ?>" == "bull") {
					var msg = "您是否确定以上交易？";
					var bull_type = $("input[name='bull_type']:checked").val();
					var price_type = $("input[name='price_type']:checked").val();
					
					//股票类型
					if (bull_type == 1) {
						rmsg += "融资";
					} else {
						rmsg += "融券";
					}
					
					//交易类型
					if (price_type == 0) {
						rmsg += "市价";
					} else {
						rmsg += "限价";
					}
					rmsg += "委托处理成功！";
				} else {
					var msg = "您是否确执行该操作？";
				}
				data += '&token='+token;
				layer.confirm(msg, {icon: 3}, function () {
                    sub += 1;
					layer.closeAll();
                    if (sub == 1) {
                        http_request_post("", data, function (result) {
                            if (rmsg == "") {
                                layer.msg(result.msg, {time:800}, function (){
                                    if (result.data != "") {
                                        location.href = result.data;
                                    } else {
                                        location.reload();
                                    }
                                });
                            } else {
                                layer.alert(rmsg, {icon:1, closeBtn:0}, function(){
                                    if (result.data != "") {
                                        location.href = result.data;
                                    } else {
                                        location.reload();
                                    }
                                });
                            }
                        });
                    }
				});
				return false;
			} else {
				//搜索专用
				var data = $(this).serializeArray();
				var	url = "/<?php echo $common_info['controller']; ?>/<?php echo $common_info['action']; ?>";
				$.each(data, function(key, val) {
					if (val.value == "") {
						url += "/0";
					} else {
						url += "/" + val.value;
					}
				});
				location.href = url;
				return false;
			}
		})
		
		//通用post请求
		function http_request_post(url, data, callback) {
			if (url != "") {
				url = url;
			}
			$.ajax({
				type : 'post',  
				url : url,
				dataType : 'json',
				data : data,
				success : function(repones) {
					if (repones.status == 1) {
						if (typeof callback === "function") {
							callback(repones);
							return false;
						} else {
							layer.msg('请求加载数据失败，请重新加载');
						}
					} else if (repones.status == 2) {
						stock = system_info = -1
						layer.msg(repones.msg, {time:800}, function() {
							location.href = "/user/login";
						});
						return false;
					} else if (repones.status == 0) {
						token = repones.token;
						layer.msg(repones.msg);
						stock = system_info = -1
						return false;
					} else {
						layer.msg(repones.msg);
						stock = system_info = -1
						return false;
					}
				},
				error : function () {
					layer.load(1, {shade: [0.1,'#fff'], time:500}, function(){
						location.reload();
					});
					return false;
				}
			});
			return false;
		}
	</script>
</html>